package com.gitee.zhangchenyan.takin.common.inject;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;


/**
 * @author zhanglu
 * @date 2019-04-09
 */
@Slf4j
@Component
public class OkHttpService {
    private static final MediaType MediaTypeJson = MediaType.parse("application/json; charset=utf-8");
    private static final MediaType MediaTypeXml = MediaType.parse("application/xml; charset=utf-8");

    private final OkHttpClient okHttpClient;

    public OkHttpService(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    public String doGet(String url, Map<String, String> params, Map<String, String> headers) {
        StringBuilder sb = new StringBuilder(url);
        if (params != null && params.keySet().size() > 0) {
            boolean firstFlag = true;
            for (String key : params.keySet()) {
                if (firstFlag) {
                    sb.append("?").append(key).append("=").append(params.get(key));
                    firstFlag = false;
                } else {
                    sb.append("&").append(key).append("=").append(params.get(key));
                }
            }
        }
        Request.Builder builder = new Request.Builder().url(sb.toString()).get();
        return execute(builder, headers);
    }


    public String doPost(String url, Map<String, String> params, Map<String, String> headers) {
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                builder.add(key, params.get(key));
            }
        }
        Request.Builder builder1 = new Request.Builder().url(url).post(builder.build());
        return execute(builder1, headers);
    }



    public String doPostJson(String url, String json, Map<String, String> headers) {
        return executePost(url, json, MediaTypeJson, headers);
    }


    public String doPostXml(String url, String xml, Map<String, String> headers) {
        return executePost(url, xml, MediaTypeXml, headers);
    }


    private String executePost(String url, String data, MediaType contentType, Map<String, String> headers) {
        RequestBody requestBody = RequestBody.create(contentType, data);
        Request.Builder builder = new Request.Builder().url(url).post(requestBody);
        return execute(builder, headers);
    }

    @SneakyThrows
    private String execute(Request.Builder builder, Map<String, String> headers) {
        if (headers != null && headers.size() > 0) {
            for (val header : headers.entrySet()) {
                builder.addHeader(header.getKey(), header.getValue());
            }
        }
        Request request = builder.build();
        log.debug(StrUtil.format("地址-[{}]-方法[{}]-body[{}]", request.url().toString(), request.method(), request.body() != null ? request.body().toString() : ""));
        Response response = okHttpClient.newCall(request).execute();
        String body = response.body().string();
        log.debug("返回-" + body);
        return body;
    }

}
